<?xml version="1.0" encoding="UTF-8" ?>
<!--

    Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
    under one or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information regarding copyright
    ownership. Camunda licenses this file to you under the Apache License,
    Version 2.0; you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
  
<mapper namespace="org.camunda.bpm.engine.impl.persistence.entity.IdentityLinkEntity">

  <!-- INSERT IDENTITY LINK -->
  
  <insert id="insertIdentityLink" parameterType="org.camunda.bpm.engine.impl.persistence.entity.IdentityLinkEntity">
    insert into ${prefix}ACT_RU_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_DEF_ID_, TENANT_ID_, REV_ )
    values (#{id, jdbcType=VARCHAR},
            #{type, jdbcType=VARCHAR},
            #{userId, jdbcType=VARCHAR},
            #{groupId, jdbcType=VARCHAR},
            #{taskId, jdbcType=VARCHAR},
            #{processDefId, jdbcType=VARCHAR},
            #{tenantId, jdbcType=VARCHAR},
            1
           )
            
  </insert>
  
  <!-- IDENTITY LINK DELETE -->
  
  <delete id="deleteIdentityLink" parameterType="string">
    delete from ${prefix}ACT_RU_IDENTITYLINK where ID_ = #{id}
  </delete>
  
  <delete id="deleteIdentityLinkByProcDef" parameterType="string">
    delete from ${prefix}ACT_RU_IDENTITYLINK where PROC_DEF_ID_ = #{id}
  </delete>
  

  <!-- IDENTITY LINK RESULTMAP -->

  <resultMap id="identityLinkResultMap" type="org.camunda.bpm.engine.impl.persistence.entity.IdentityLinkEntity">
    <id property="id" column="ID_" jdbcType="VARCHAR" />
    <result property="type" column="TYPE_" jdbcType="VARCHAR" />
    <result property="userId" column="USER_ID_" jdbcType="VARCHAR" />
    <result property="groupId" column="GROUP_ID_" jdbcType="VARCHAR" />
    <result property="taskId" column="TASK_ID_" jdbcType="VARCHAR" />
    <result property="processDefId" column="PROC_DEF_ID_" jdbcType="VARCHAR" />
    <result property="tenantId" column="TENANT_ID_" jdbcType="VARCHAR" />
  </resultMap>

  <!-- IDENTITY LINK SELECT -->

  <select id="selectIdentityLink" parameterType="string" resultMap="identityLinkResultMap">
    select * from ${prefix}ACT_RU_IDENTITYLINK where ID_ = #{id}
  </select>
  
  <select id="selectIdentityLinksByTask" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="identityLinkResultMap">
    select * from ${prefix}ACT_RU_IDENTITYLINK where TASK_ID_ = #{parameter}
  </select>
  
  <select id="selectIdentityLinksByProcessDefinition" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="identityLinkResultMap">
    select * from ${prefix}ACT_RU_IDENTITYLINK where PROC_DEF_ID_ = #{parameter}
  </select>
  
  <select id="selectIdentityLinkByTaskUserGroupAndType" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="identityLinkResultMap">
    select * 
    from ${prefix}ACT_RU_IDENTITYLINK 
    where TASK_ID_ = #{parameter.taskId}
      <if test="parameter.userId != null">
        and USER_ID_ = #{parameter.userId}
      </if>
      <if test="parameter.groupId != null">
        and GROUP_ID_ = #{parameter.groupId}
      </if>
      <if test="parameter.type != null">
        and TYPE_ = #{parameter.type}
      </if>
  </select>
  
  <select id="selectIdentityLinkByProcessDefinitionUserAndGroup" parameterType="org.camunda.bpm.engine.impl.db.ListQueryParameterObject" resultMap="identityLinkResultMap">
    select * 
    from ${prefix}ACT_RU_IDENTITYLINK 
    where PROC_DEF_ID_ = #{parameter.processDefinitionId}
      <if test="parameter.userId != null">
        and USER_ID_ = #{parameter.userId}
      </if>
      <if test="parameter.groupId != null">
        and GROUP_ID_ = #{parameter.groupId}
      </if>
  </select>
  
</mapper>
